package com.shuyou.tabui;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.shuyou.BookInfoActivity;
import com.shuyou.R;
import com.shuyou.lib.BooksDesListView;
import com.shuyou.lib.BooksDesListViewAdapter;
import com.shuyou.lib.PullToRefreshBase.OnRefreshListener;
import com.shuyou.lib.PullToRefreshListView;
import com.shuyou.net.BookDetail;
import com.shuyou.net.Data;
import com.shuyou.net.JsonParser;
import com.shuyou.net.ServerInfo;
import com.shuyou.net.StaticHttpClient;
import com.shuyou.net.UserInfo;
import com.shuyou.values.Share;
import com.shuyou.values.Values;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TopNewShuyouTab extends Activity{

	private Intent i = null;
	private Data   data = null;
	private ArrayList<BookDetail> booklist = null;
	private HashMap<String, String> map = null;
	private ServerInfo si = null;
	private TextView listFooter = null;
	
	
	private PullToRefreshListView refreshList = null;
	private ListView list = null;
	private BooksDesListViewAdapter booksDesListAdapter = null;
	private boolean isRefreshed = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shuyou_tab_topnew);
		
		/*排列按最新排序的书籍信息列表*/
		i = getIntent();
		data = (Data) i.getSerializableExtra("bookinfo");
		refreshList = (PullToRefreshListView)findViewById(R.id.topnew_list);
		list = refreshList.getRefreshableView();
		
		refreshList.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				isRefreshed = true;
				getIndexBooks("-1");   //刷新请求第一页
			}
			
		});
		
		if(data != null){
			DisplayContent();
		}
		/***为TopHotShuyouTab所用***/
		Share.loadingHotCode = 0;
	}
	/**
	 * 把获取到的内容在界面中显示出来
	 */
	private void DisplayContent(){
		//创建一个List集合， List集合元素是BooksDesListView
		List<BooksDesListView> booksDesList = new ArrayList<BooksDesListView>();
		if (data != null) {
			booklist = data.getBook_list();
			for (BookDetail ib : booklist) {
				/*标签项最多只显示三个*/
				String show_label = null;
				String []labels = ib.getLabel().split(";");
				if(labels.length <= 3) {
					show_label=ib.getLabel();
				} else {
					show_label=labels[0]+";"+labels[1]+";"+labels[2];
				}
				/*书名名最多显示10个字符*/
				String showBookName = null;
				if(ib.getBook_name().length() > 10) {
					showBookName = ib.getBook_name().substring(0, 10) + "..";
				} else {
					showBookName = ib.getBook_name();
				}
				/*人名最多显示10个字符*/
				String showAuthorName = null;
				if(ib.getAuthor().length() > 10) {
					showAuthorName = ib.getAuthor().substring(0, 10) + "..";
				} else {
					showAuthorName = ib.getAuthor();
				}
				
				BooksDesListView booksDes = new BooksDesListView(ib.getBook_link(), showBookName, showAuthorName,
						show_label, ib.getCurrent_available_count());
				booksDesList.add(booksDes);
			}
		}
        
		booksDesListAdapter = new BooksDesListViewAdapter(TopNewShuyouTab.this, 
				                                    booksDesList, list);
		
		listFooter = new TextView(TopNewShuyouTab.this);
		/*设置低步加载更多风格*/
		listFooter.setBackgroundDrawable(getResources().getDrawable(R.drawable.shuyou_listfooter_bg));
		listFooter.setShadowLayer((float)0.5, (float)1, (float)1, R.color.black);
		listFooter.setPadding(30, 15, 0, 10);
		listFooter.setGravity(Gravity.CENTER_VERTICAL);
		listFooter.setText("加载更多");
		listFooter.setTextSize((float) 20.0);
		listFooter.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.v("LAST_TIME", "last_time" + data.getLast_time());
				getIndexBooks(data.getLast_time());
				listFooter.setText("正在加载...");
				listFooter.setEnabled(false);
			}
			
		});
		if(booklist.size() < 20) {
			listFooter.setText("已全部加载");
			listFooter.setEnabled(false);
		}
		
		list.addFooterView(listFooter);
		
		list.setDivider(null);
		list.setAdapter(booksDesListAdapter);
		
		list.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				Intent i = new Intent();
				i.putExtra("bookIsbn", booklist.get(position).getIsbn());
				i.putExtra("bookName", booklist.get(position).getBook_name());
				i.putExtra("bookAuthor", booklist.get(position).getAuthor());
				i.putExtra("bookLabel", booklist.get(position).getLabel());
				i.setClass(TopNewShuyouTab.this, BookInfoActivity.class);
				startActivity(i);
			}
		});
	}
	
	/**
	 * 加载更多最新图书，每次加载20本
	 * @param last_time
	 */
	private void getIndexBooks(final String last_time) {
		map = new HashMap<String, String>();
		//TODO
		map.put("sessionid", UserInfo.sessionid);
		map.put("books_type", "new");
		map.put("platform_userid", UserInfo.platform_userid);
		map.put("last_time", last_time);
		
		new Thread() {

			@Override
			public void run() {
				try {
					String strResult = StaticHttpClient.doGet(Values.GET_INDEX_BOOK , map);
					if (strResult == null) {
						System.out.println("null");
					}
					si = (ServerInfo) JsonParser.parseObject(strResult,
							ServerInfo.class);//需要处理空指针异常
					System.out.println(si);
					String status = si.getStatus();
					if(status.equals("10000"))//操作正确
					{
						Message msg = new Message();
						msg.what = Values.NEW_NEXT_LOADING_SUCCESS;
						msg.obj = si.getData();
						handler.sendMessage(msg);
					} else if(status.equals("10002")) {//参数错误
						Message msg = new Message();
						msg.what = Values.NEW_LOADING_FAIL;
						msg.obj = "参数错误";
						handler.sendMessage(msg);
					} else if(status.equals("10003")) {
						Message msg = new Message();
						msg.what = Values.NEW_LOADING_FAIL;
						msg.obj = "服务器内部错误";
						handler.sendMessage(msg);
					} else if(status.equals("10004")) {
						Message msg = new Message();
						msg.what = Values.NEW_LOADING_FAIL;
						msg.obj = "无效的session";
						handler.sendMessage(msg);
					} else {
						Message msg = new Message();
						msg.what = Values.NEW_LOADING_FAIL;
						msg.obj = "未知错误";
						handler.sendMessage(msg);
					}
					
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					Message msg = new Message();
					msg.what = Values.NETWORK_ERROR;
					handler.sendMessage(msg);
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Message msg = new Message();
					msg.what = Values.NETWORK_ERROR;
					handler.sendMessage(msg);
					e.printStackTrace();
				}
				
			}

		}.start();

	}
	

	 /**
	  * 成功加载后执行的动作
	  * @param tData
	  */
	 private void LoadingSuccess(Data tData) {
			data = tData;
			if(data.getBook_list() == null) {
				return ;
			}
				
			//如果是刷新的，则先情况以前的数据再显示
			if(isRefreshed) {
				booksDesListAdapter.clear();
				booklist.clear();
				booksDesListAdapter.notifyDataSetChanged();
				isRefreshed = false;
				listFooter.setText("加载更多");
				listFooter.setEnabled(true);
				
			}
			if(data.getBook_list().size() < 20) {
				listFooter.setText("已全部加载");
				listFooter.setEnabled(false);
			} else {
				listFooter.setText("加载更多");
				listFooter.setEnabled(true);
			}
			int count = 0;
			for (BookDetail ib : data.getBook_list()) {
				booklist.add(ib);
				/*标签项最多只显示三个*/
				String show_label = null;
				String []labels = ib.getLabel().split(";");
				if(labels.length <= 3) {
					show_label=ib.getLabel();
				} else {
					show_label=labels[0]+";"+labels[1]+";"+labels[2];
				}
				/*书名名最多显示10个字符*/
				String showBookName = null;
				if(ib.getBook_name().length() > 10) {
					showBookName = ib.getBook_name().substring(0, 10) + "..";
				} else {
					showBookName = ib.getBook_name();
				}
				/*人名最多显示10个字符*/
				String showAuthorName = null;
				if(ib.getAuthor().length() > 10) {
					showAuthorName = ib.getAuthor().substring(0, 10) + "..";
				} else {
					showAuthorName = ib.getAuthor();
				}
				
				BooksDesListView booksDes = new BooksDesListView(ib.getBook_link(), showBookName, showAuthorName,
						show_label, ib.getCurrent_available_count());
				booksDesListAdapter.add(booksDes);
				booksDesListAdapter.notifyDataSetChanged();
				++ count;
			}
			for(int i = count; i < Values.MIN_LINE_DISPLAY; ++i)   //不足四行的，要补气四行
	    	{
	    		BooksDesListView booksDes = new BooksDesListView("null", "null", "null","null", "null");
				booksDesListAdapter.add(booksDes);
				booksDesListAdapter.notifyDataSetChanged();
	    	}
	 }
	 
	 /**
	 * 静态handler,防止内存泄漏错误
	 * @author chris
	 *
	 */
	 static class MyInnerHandler extends Handler {
        WeakReference<TopNewShuyouTab> mActivity;

        MyInnerHandler(TopNewShuyouTab mAct) {
        	mActivity = new WeakReference<TopNewShuyouTab>(mAct);
        }

        @Override
        public void handleMessage(Message msg) {
        	TopNewShuyouTab theAct = mActivity.get();
            switch (msg.what) {
            case Values.NEW_NEXT_LOADING_SUCCESS:
				theAct.LoadingSuccess((Data)msg.obj);
				break;
			case Values.NETWORK_ERROR:
				Toast.makeText(theAct, "网络连接异常", Toast.LENGTH_SHORT).show();
				theAct.listFooter.setEnabled(true);
				theAct.listFooter.setText("加载更多");
				break;
			default:
				break;
			}
            theAct.refreshList.onRefreshComplete();
	    }
	 }
	 MyInnerHandler handler = new MyInnerHandler(this);
}
